﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity.Spatial;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TreeManagement.Querry
{
    public partial class SearchTree : Form
    {
        public SearchTree()
        {
            InitializeComponent();
        }

        private void SearchTree_Load(object sender, EventArgs e)
        {
            updateControls();
            using (var context = new TreeDBEntities())
            {
                cbbWard.DataSource = context.Wards.ToList();
                cbbNearestWard.DataSource = cbbWard.DataSource;
                cbbRoad.DataSource = context.Roads.ToList();
                cbbTreeType.DataSource = context.TreeTypes.ToList();

                viewPanel1.WardPoints = TMUtils.getWardPoints(context);
                viewPanel1.RoadPoints = TMUtils.getRoadPoints(context);
                viewPanel1.Trees = TMUtils.getTreePoint(context);
                viewPanel1.Refresh();
            }
        }
        private void updateControls()
        {
            if (cbRoad.Checked)
                cbbRoad.Enabled = true;
            else
                cbbRoad.Enabled = false;

            if (cbTreeType.Checked)
                cbbTreeType.Enabled = true;
            else
                cbbTreeType.Enabled = false;

            if (cbWard.Checked)
                cbbWard.Enabled = true;
            else
                cbbWard.Enabled = false;

            if (cbNearestWard.Checked)
                cbbNearestWard.Enabled = true;
            else
                cbbNearestWard.Enabled = false;

            if (cbR.Checked)
                panel1.Enabled = true;
            else
                panel1.Enabled = false;

            if (cbNearest.Checked)
                cbbNearestWard.Enabled = true;
            else
                cbbNearestWard.Enabled = false;
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            viewPanel1.RRect = null;

            using (var context = new TreeDBEntities())
            {
            string sql = "SELECT * FROM TREE WHERE 1 = 1  ";
            if (cbTreeType.Checked)
                sql += " AND Type = " + (cbbTreeType.SelectedItem as TreeType).ID.ToString();
            if (cbRoad.Checked)
                sql += " AND  Road = " + (cbbRoad.SelectedItem as Road).ID.ToString();
            if (cbWard.Checked)
                sql += " AND  Ward = " + (cbbWard.SelectedItem as Ward).ID.ToString();
            if (cbR.Checked)
            {
                DbGeometry geo = context.Trees.Find(Convert.ToInt32( txtID.Text)).Point;
                int R = Convert.ToInt32(txtR.Text);
                List<Tree> trees = context.FindTreeInR(geo, R).ToList<Tree>();
                dataGridView1.DataSource = trees;
                Tuple<Point, int> typle = new Tuple<Point, int>(new Point((int)geo.XCoordinate,(int) geo.YCoordinate), R);
                viewPanel1.RRect = typle;
                viewPanel1.Refresh();
                return;
            }
            if (cbNearest.Checked)
            {
                DbGeometry geo = context.Trees.Find(Convert.ToInt32(txtNearestID.Text)).Point;
                List<Tree> trees = context.FindNearestTreeInR(geo).ToList<Tree>();
                if (trees.Count > 1)
                {
                    List<Tree> visibleData = new List<Tree>();
                    visibleData.Add(trees[1]);
                    dataGridView1.DataSource = visibleData;
                    if (trees.Count > 0)
                    {
                        List<Point> p = new List<Point>();
                        p.Add(new Point((int)geo.XCoordinate, (int)geo.YCoordinate));
                        p.Add(new Point((int)visibleData[0].Point.XCoordinate, (int)visibleData[0].Point.YCoordinate));
                        viewPanel1.Trees = p;
                        viewPanel1.Refresh();
                    }
                }
                return;
            }
            if (cbNearestWard.Checked)
            {
                DbGeometry wardGeo = (cbbNearestWard.SelectedItem as Ward).Polygon;
                List<Tree> trees = context.FindNearestTreeInWard(wardGeo).ToList<Tree>();
                
                List<Point> wardPointList = TMUtils.getWardPoints(context,(cbbNearestWard.SelectedItem as Ward).ID);
                List<List<Point>>wardPointLists = new List<List<Point>>();
                wardPointLists.Add(wardPointList);
                if (trees.Count > 1)
                {
                    List<Tree> visibleData = new List<Tree>();
                    visibleData.Add(trees[1]);
                    dataGridView1.DataSource = visibleData;
                    if (trees.Count > 0)
                    {
                        List<Point> p = new List<Point>();
                        p.Add(new Point((int)visibleData[0].Point.XCoordinate, (int)visibleData[0].Point.YCoordinate));
                        viewPanel1.Trees = p;
                        viewPanel1.WardPoints = wardPointLists;
                        viewPanel1.Refresh();
                    }
                }
                return;

            }
                dataGridView1.DataSource = context.Trees.SqlQuery(sql, new object[] { }).ToList() ;
                viewPanel1.WardPoints = TMUtils.getWardPoints(context);
                viewPanel1.RoadPoints = TMUtils.getRoadPoints(context);
                viewPanel1.Trees = TMUtils.getTreePoint(context);
                viewPanel1.Refresh();
            }
        }

        private void cbTreeType_CheckedChanged(object sender, EventArgs e)
        {
            updateControls();
        }
    }
}
